<!DOCTYPE html>
<html>
  <head>
	<title>Online Stock Market</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<meta charset="UTF-8">
	<link rel="stylesheet" href="css/wpl_style.css">
	<link rel="stylesheet" href="css/bootstrap.min.css">
	<link rel="stylesheet" href="css/datepicker3.css">
	<link rel="stylesheet" type="text/css" href="css/tooltipster.css" />
	<script src="js/jquery-1.11.1.min.js"></script>
	<script type="text/javascript" src="js/jquery.tooltipster.min.js"></script>
	
	
	<script src="js/bootstrap.min.js"></script>
	<script src="js/bootstrap-datepicker.js"></script>
	<?php include './php/dbInclude.php';?>

  </head>

  <body>
  <div class="container" id="container">
		<div id="header">
			<div id ="logo">
			<a href="./index.php">
			<img src="./images/PM9RTp1416879776.png"> </img>
			</a>
			</div>
			<div id ="headName">
			
			
			
			</div>
		</div>
		<div id="content">
  <?php
// define variables and set to empty values
$fnameErr = $lnameErr= $emailErr = $addrErr = $dobErr = $phnErr = $passwordErr = "";
$fname = $lname = $email = $addr = $dob = $phn = $password = $confPassword = "";
$flag = 1;
$flagNew = 0;

if ($_SERVER["REQUEST_METHOD"] == "POST") {
   if (empty($_POST["first_name"])) {
     $fnameErr = " First Name is required";
	 $flag = 0;
   } else {
     $fname = test_input($_POST["first_name"]);
     // check if name only contains letters and whitespace
     if (!preg_match("/^[a-zA-Z ]*$/",$fname)) {
       $fnameErr = "Only letters and white space allowed";
	   $flag = 0;
     }
   }

   if (empty($_POST["last_name"])) {
     $lnameErr = " Last Name is required";
	  $flag = 0;
   } else {
     $lname = test_input($_POST["last_name"]);
     // check if name only contains letters and whitespace
     if (!preg_match("/^[a-zA-Z ]*$/",$lname)) {
       $lnameErr = "Only letters and white space allowed";
	    $flag = 0;
     }
   }
   
   if (empty($_POST["email"])) {
     $emailErr = "Email is required";
	  $flag = 0;
   } else {
     $email = test_input($_POST["email"]);
     // check if e-mail address is well-formed
     if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
       $emailErr = "Invalid email format";
	    $flag = 0;
     }
   }
	
	if (empty($_POST["address"])) {
	 $addrErr = "Address is required";
	 $flag = 0;
	} else {
	 $addr = test_input($_POST["address"]);
	}
   
   if (empty($_POST["dob"])) {
	 $dobErr = "Date of Birth is required";
	 $flag = 0;
	} else {
	 $dob = test_input($_POST["dob"]);
	}
   
   if (empty($_POST["phone"])) {
     $phnErr = " Phone Number is required";
	  $flag = 0;
   } else {
     $phn = test_input($_POST["phone"]);
    if(!preg_match("/^[0-9]{3}-[0-9]{3}-[0-9]{4}$/", $phn)) {
	  $phnErr = "Invalid phone number format";
	   $flag = 0;
	}
   }
   
   if (empty($_POST["password"])||empty($_POST["password_confirmation"])) {
     $passwordErr = "Password is required";
	  $flag = 0;
   } else {
     $password = test_input($_POST["password"]);
	 $confPassword = test_input($_POST["password_confirmation"]);
	 
     if(!preg_match('/^(?=.*\d)(?=.*[A-Za-z])[0-9A-Za-z!@#$%]{8,12}$/', $password)) {
		$passwordErr = "Please use according to the specified requirements";
		 $flag = 0;
	 }
	 else if(strcasecmp($password, $confPassword) != 0) {
		$passwordErr = "The passwords doesn't match";
		 $flag = 0;
	 }
	 
   }
	$flagNew = $flag;
	if($flag == 1){
		try {  
		  $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

			$sql = 'CALL add_user(:fname, :lname, :phone, :email, :address, :dob, :password, @code, @msg, @clientID)';
			$stmt = $dbh->prepare($sql);
		 
			$stmt->bindParam(':fname', $fname, PDO::PARAM_STR);
			$stmt->bindParam(':lname', $lname, PDO::PARAM_STR);
			$stmt->bindParam(':phone', $phn, PDO::PARAM_STR);
			$stmt->bindParam(':email', $email, PDO::PARAM_STR);
			$stmt->bindParam(':address', $addr, PDO::PARAM_STR);
			$stmt->bindParam(':dob', $dob, PDO::PARAM_STR);
			$stmt->bindParam(':password', $password, PDO::PARAM_STR);
			
			$stmt->execute();
			$stmt->closeCursor();
			// execute the second query to get customer's level
			$r = $dbh->query("SELECT @code AS code, @msg AS msg, @clientID AS clientID")->fetch(PDO::FETCH_ASSOC);
			if ($r) {
				// echo "Returned code is".$r['code'];
				// echo "Returned Msg is".$r['msg'];
				// echo "Returned clientID is".$r['clientID'];
				$code = $r['code'];
				$msg = $r['msg'];
				if($code == "S"){
					echo "<h4>Thank you for registering. The Admin will now link your account to your bank account and it take around 3 hours for it. </h4>";
					echo "<h4>Please click <a href='./index.php'>here</a> to go to login page</h4>";
				}else{
					echo "<h4>".$msg."</h4>";
					echo "<h4>Please click <a href='./index.php'>here</a> to go to login page</h4>";
				}
			}
		} catch (Exception $e) {
		  $dbh->rollBack();
		  echo "Failed: " . $e->getMessage();
		}
		
	
	}
   

  
}
if($flagNew==0){
?>
		
		
		<div id="loginForm">
	  	<form role="form" autocomplete="off" method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
			    			<div class="row">
			    				<div class="col-xs-6 col-sm-6 col-md-6">
			    					<div class="form-group">
										<input type="text" name="first_name" id="first_name" value="<?php echo $fname;?>" class="form-control " placeholder="First Name">
			    					</div>
			    				</div>
			    				<div class="col-xs-6 col-sm-6 col-md-6">
			    					<div class="form-group">
			    						<input type="text" name="last_name" id="last_name" value="<?php echo $lname;?>" class="form-control " placeholder="Last Name">
			    					</div>
			    				</div>
			    			</div>
							<div class="row">
			    				<div class="col-xs-6 col-sm-6 col-md-6">
			    					<div class="form-group">
									 <span class="error"><?php echo $fnameErr;?></span>
			    					</div>
			    				</div>
			    				<div class="col-xs-6 col-sm-6 col-md-6">
			    					<div class="form-group">
			    					 <span class="error"><?php echo $lnameErr;?></span>
			    					</div>
			    				</div>
			    			</div>

			    			<div class="form-group">
			    				<input type="email" name="email" id="email" class="form-control " value="<?php echo $email;?>" placeholder="Email Address">
			    			</div>
							
							<div class="form-group">
			    				<span class="error"><?php echo $emailErr;?></span>
			    			</div>
							
							<div class="form-group">
							  <input type="text" class="form-control" name="address" id="address" value="<?php echo $addr;?>" placeholder="Address (7825, McCallum Blvd., #999, Dallas, TX-75252)">
							</div>
							
							<div class="form-group">
			    				<span class="error"><?php echo $addrErr;?></span>
			    			</div>
							
							<div class="form-group">
									<div class="input-group date">
									<input type="text" placeholder="Date of Birth" value="<?php echo $dob;?>" readonly="readonly" name="dob" class="form-control"><span class="input-group-addon"><i class="glyphicon glyphicon-th"></i></span>
									</div>
							</div>
							
							<div class="form-group">
			    				<span class="error"><?php echo $dobErr;?></span>
			    			</div>
							
							<div class="form-group">
							  <input type="text" class="form-control" name="phone" value="<?php echo $phn;?>" id="phone" placeholder="Phone No (999-123-4567)">
							</div>
							
							<div class="form-group">
			    				<span class="error"><?php echo $phnErr;?></span>
			    			</div>
							
			    			<div class="row">
			    				<div class="col-xs-6 col-sm-6 col-md-6">
			    					<div class="form-group">
			    						<input type="password" name="password" id="password" class="tooltip form-control" placeholder="Password" style="opacity:1;position:relative;font-size: 14px;font-family: inherit;"  title="Password field must be at least 8 chars long containing 1 capital letter, 1 number and 1 special Character. " >
			    					</div>
			    				</div>
			    				<div class="col-xs-6 col-sm-6 col-md-6">
			    					<div class="form-group">
			    						<input type="password" name="password_confirmation" id="password_confirmation" class="form-control " placeholder="Confirm Password">
			    					</div>
			    				</div>
			    			</div>
			    			<div class="form-group">
			    				<span class="error"><?php echo $passwordErr;?></span>
			    			</div>
			    			<input type="submit" value="Register" class="btn btn-info btn-block">
			    		
			    		</form>
		</div>



<?php



}

function test_input($data) {
   $data = trim($data);
   $data = stripslashes($data);
   $data = htmlspecialchars($data);
   return $data;
}
?>
  
  
    
		
	</div>
		
    </div>
<div class="footer_bg"><!-- start footer -->
			<div class="container">
				<div class="row  footer">
					<div class="copy text-center">
						<p class="link"><span>&#169; All rights reserved | Design by&nbsp;<a href="./about.html"> TEAM 11</a></span></p>
					</div>
				</div>
			</div>
		</div>	
    
  </body>
  <script>
  $(document).ready(function(){
      $('.input-group.date').datepicker({
		autoclose: true
		});
  });
  
  $(document).ready(function() {
            $('.tooltip').tooltipster({
			
			});
        });
  </script>
</html>
